package com.leo.version4;

/**
 * The MIT License
 * <p>
 * Copyright (c) 2016 Shell Technologies PTY LTD. http://au.shellpays.com
 * <p>
 * You may obtain a copy of the License at
 * <p>
 * http://mit-license.org/
 */

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Steven Chen
 *
 */
public class FontawesomeWithJavaFX extends Application {
    private static final Font GLYPH_FONTAWESOME;
    private static final Map<String, Character> GLYPH_MAP;

    static {
        // 加载字体库文件
        GLYPH_FONTAWESOME = Font
                .loadFont(FontawesomeWithJavaFX.class.getResourceAsStream("/font/fontawesome-webfont.ttf"), -1);
        GLYPH_MAP = new HashMap<String, Character>();
        GLYPH_MAP.put("fa_500px", '\uf26e');
        GLYPH_MAP.put("anchor", '\uf13d');
        GLYPH_MAP.put("youtube_play", '\uf16a');
        GLYPH_MAP.put("youtube_square", '\uf166');
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        launch(args);
    }

    private ScrollPane scrollPane;
    private FlowPane iconPane;
    private List<Button> buttonList;

    @Override
    public void init() throws Exception {
        buttonList = GLYPH_MAP.keySet().stream().map(i -> {
            return createGlyphButton(i, 2);
        }).collect(Collectors.toList());
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        HBox topPane = new HBox();
        topPane.setAlignment(Pos.BASELINE_CENTER);
        topPane.setPadding(new Insets(16));
        topPane.setSpacing(10);
        Text title = new Text("Icon Button List");
        title.setFont(Font.font(24));
        topPane.getChildren().addAll(title);
        HBox.setHgrow(title, Priority.ALWAYS);
        scrollPane = new ScrollPane();
        scrollPane.setPadding(new Insets(16));
        iconPane = new FlowPane();
        iconPane.getChildren().addAll(buttonList);
        scrollPane.setContent(iconPane);
        scrollPane.setVvalue(0.0);
        iconPane.prefWidthProperty().bind(scrollPane.widthProperty().subtract(20.0));
        BorderPane root = new BorderPane();
        root.setTop(topPane);
        root.setCenter(scrollPane);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Fontawesome Glyph");
        primaryStage.setWidth(800);
        primaryStage.setHeight(600);
        primaryStage.show();
    }

    private static Button createGlyphButton(String glyphName, int sizeFactor) {
        Label lbl = new Label();
        // 使用自定义字体
        lbl.setFont(Font.font(GLYPH_FONTAWESOME.getFamily(), 8 * sizeFactor));
        // 创建并使用填充色(可选)
        Stop[] stops = new Stop[]{new Stop(0, Color.BLACK), new Stop(1, randomColor())};
        LinearGradient lg1 = new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stops);
        lbl.setText(String.valueOf(GLYPH_MAP.get(glyphName)));
        lbl.setTextFill(lg1);
        Button btn = new Button(glyphName, lbl);
        btn.setWrapText(true);
        btn.setContentDisplay(ContentDisplay.TOP);
        return btn;
    }

    private static Color randomColor() {
        return new Color(Math.random(), Math.random(), Math.random(), 1.0);
    }
}
